<template>
  <view>
    <fa-navbar :title="vuex_table_title || '编辑简介'"></fa-navbar>
<!--    <view class="u-p-30 mtop">-->
<!--    <u-form-item label="微信:" prop="username" label-width="150">-->
<!--      <u-input v-model="wx" />-->
<!--    </u-form-item>-->
<!--    <u-form-item label="邮箱:" prop="username" label-width="150">-->
<!--      <u-input v-model="email" />-->
<!--    </u-form-item>-->
<!--    </view>-->
    <cu-editor ref="editor" :maxSize="maxSize" :url="uploadUrl" :header="header" :formData="formData" :content="content" @before="onUploadBefore" @update="onUpdate" @save="onSave"></cu-editor>
  </view>
</template>

<script>
import cuEditor from '@/components/cu-editor/cu-editor'
import { handleHtmlImage } from '@/components/cu-editor/util'

export default {
  components: {
    cuEditor
  },
  data() {
    return {
      wx:'',
      email:'',
      content: '',
      uploadUrl: '',
      header: {},
      formData: {},
      maxSize:5
    }
  },
  onLoad(options) {

    uni.setNavigationBarTitle({
      title: "编辑简介"
    })

    uni.hideShareMenu()

    var that = this;

    console.log('token:',this.vuex_token);

    if (!this.vuex_token) {
      this.$u.vuex('vuex_user', {});
    }

    console.log(this.vuex_user)


    this.uploadUrl = this.vuex_config.upload && ( this.vuex_config.upload.uploadurl || this.$image_url+'/api/common/upload')

    this.header.token = this.vuex_token || ''

    this.header.uid = this.vuex_user.id || 0

    this.content = this.vuex_user.profile || ''

    // if(options.item){
    //   const item = JSON.parse(decodeURIComponent(options.item));
    //   item.content ? this.content = item.content : ''
    // }
    // wx.enableAlertBeforeUnload({
    //   message: "询问对话框内容"
    // })

    uni.$on('save',function(res){

      that.updateContent(res.html)

    })

    //this.getUserIndex();

  },
  methods: {
    onUploadBefore: function() {
      this.formData = {
        timestamp: (Date.parse(new Date())) / 1000
      }
    },
    onUpdate(res) {
      res.html = handleHtmlImage(res.html)
      this.value = res.html
    },
    //保存
    onSave(e) {
      let html = e.html
      var containsImage = html.search(/<img /i) >= 0 //内容是否包含图片标签
      let txt = e.text.replace(/(^\s*)|[\r\n]|(\s*$)/g, "") //去掉换行符和两端空格

      uni.$emit('save', {
        html: txt == "" && !containsImage ? txt : html
      })
    },
    updateContent: async function(content){

      let res = await this.$api.updateUserIntro({'content':content});

      if(res.code==1){

        this.vuex_user.profile = content

        this.$u.vuex('vuex_user',this.vuex_user);

        // uni.redirectTo({
        //   url: '/pages/index/card'
        // });

        this.$u.route('/pages/my/profile');


      }



    }

  }
}
</script>

<style>
.mtop{
  margin-top: 108rpx;
}
</style>